<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="名称" prop="pocessName">
        <el-input
          ref="input"
          v-model="queryParams.pocessName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">



        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activiti:pocess:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activiti:pocess:edit']"
        >修改</el-button>
      </el-col>



      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activiti:pocess:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activiti:pocess:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pocessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="pocessId" />
      <el-table-column label="名称" align="center" prop="pocessName" />
      <el-table-column label="邮件模板" align="center" prop="mailTemplate" />

      <el-table-column label="备注" align="center" prop="remark" />
       <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['activiti:pocess:edit']"
          >详情</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePoces(scope.row)"
            v-hasPermi="['activiti:pocess:edit']"
          >提交</el-button>

         <el-button
           size="mini"
           type="text"
           icon="el-icon-edit"
           @click="historicActivity (scope.row)"
           v-hasPermi="['workflow:leave:edit']"
         >追溯
         </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activiti:pocess:edit']"
          >修改</el-button>




          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activiti:pocess:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程对话框 详情页-->
    <el-dialog :title="detailDialog.title" :visible.sync="detailDialog.open" width="700px" append-to-body>
       <pocessItem
        :open.sync="detailDialog.open"
        :id="detailDialog.id"
        :isDisabled.sync="detailDialog.isDisabled"
        @doRefresh="getList"  > </pocessItem>

         <pocesStart  v-if="detailDialog.hasPoces"
         :open.sync="detailDialog.open"
         :befinitionKey="detailDialog.befinitionKey"
         :processDefinitionId="detailDialog.processDefinitionId"
         :id="detailDialog.id"
         :vars="detailDialog.vars"
         @doRefresh="getList"
          ></pocesStart>
    </el-dialog>


    <el-dialog :title="historicDialog.title" :visible.sync="historicDialog.open" width="1000px" append-to-body>
        <div class="chart-wrapper">
        <pocessHistoric
          :id.sync="historicDialog.id"
          :open.sync="historicDialog.open"
          :businessId.sync="historicDialog.businessId"
          :befinitionKey.sync="historicDialog.befinitionKey"
        >
        </pocessHistoric>
         </div>
    </el-dialog>

  </div>
</template>

<script>
import pocesStart from "@/views/activiti/pocess/pocesStart.vue";
import pocessHistoric from "@/views/activiti/pocess/pocessHistoric.vue";

import pocessItem from "./pocessItem.vue";

import { listPocess, getPocess, delPocess, addPocess, updatePocess, exportPocess } from "@/api/activiti/pocess";




export default {
  name: "Pocess",
  components: { pocessItem,pocesStart ,pocessHistoric },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程表格数据
      pocessList: [],





      // 弹出层标题
      detailDialog: {
         //业务id
         id:"",
         title: "",
         open: false,

         //是否是详情页 true：是
         isDisabled : true,

         //工作流控制
         //是否弹出工作流
         hasPoces : false,
         befinitionKey :"lossevent_6",
         processDefinitionId :"lossevent_6:18:2410db58-4fed-11eb-96d2-b0359fadfbca",
         vars :{
           acti_num : 4,
           title : "工作流标题",
         },
      },

      //流程追溯
       historicDialog:{
         //  流程实例  roInstanceId
         id:"",
         title: "",
         open: false,
         // 业务id
         businessId : "",
         befinitionKey : "",
       },




      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pocessName: null,
        mailTemplate: null,
        status: null,
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
    this.getDicts("T21_flow_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询流程列表 */
    getList() {
      this.loading = true;
      this.detailDialog.hasPoces=false;
      listPocess(this.queryParams).then(response => {
        this.pocessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pocessId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.detailDialog.id="";
      this.detailDialog.open = true;
      this.detailDialog.isDisabled = false;
      this.detailDialog.hasPoces = false;
      this.detailDialog.title = "添加流程";

    },

     /** 历史 */
     historicActivity(row){

       console.log("父页面:"+row.pocessId)

       const businessId = row.pocessId
        this.historicDialog.open = true;
        this.historicDialog.title = "流程追溯";
        this.historicDialog.businessId = businessId;
         this.historicDialog.befinitionKey= this.detailDialog.befinitionKey


     },

    /** 工作流操作 */
    handlePoces(row) {
      const pocessId = row.pocessId || this.ids[0]
      this.detailDialog.id=pocessId;

      this.detailDialog.open = true;
      this.detailDialog.isDisabled = true;
      this.detailDialog.hasPoces = true;
      this.detailDialog.title = "提交审批流程";
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      const pocessId = row.pocessId || this.ids[0]
      this.detailDialog.id=pocessId;

      this.detailDialog.open = true;
      this.detailDialog.isDisabled = true;
      this.detailDialog.hasPoces = false;
      this.title = "详情流程";

    },

    /** 修改按钮操作 */
    handleUpdate(row) {
         const pocessId = row.pocessId || this.ids[0]
         this.detailDialog.id=pocessId;
         this.detailDialog.open = true;
         this.detailDialog.isDisabled = false;
         this.detailDialog.hasPoces = false;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const pocessIds = row.pocessId || this.ids;
      this.$confirm('是否确认删除流程编号为"' + pocessIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPocess(pocessIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有流程数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPocess(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
